﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agile.Engine.View;
using System.Collections;
using Agile.Engine;
using Agile.Engine.Data;
using System.Data;

namespace Fis.Win.Rules
{
    public class BookInfoProvider : ClientViewProvider
    {
        public override Hashtable InitializeViewModel(Hashtable args)
        {
            var model = args ?? new Hashtable();

            //缓存存在
            if (ViewState == Agile.Engine.View.ViewState.Modify)
            {
                model["data"] = new DataAccess() { Context = DataContext, TableName = DataTableName }.GetData();
            }
            else
            {
                model["data"] = Fis.Win.Utils.SchemaUtils.LoadSchema(DataTableName);
                model["action"] = "create";
            }

            return model;
        }

        public override bool ExecuteUpdateXtra(Hashtable viewModel)
        {
            if (Fis.Win.Utils.AppSession.IsComplateInitialize)
            {
                Message = "已结束初始化，不能进行此操作";
                return false;
            }
            else if (!Fis.Win.Utils.AppSession.Range.Equals("0"))
            {
                Message = "上年度账套不存在。";
                return false;
            }

            var param = Fis.Win.Utils.AppSession.CreateServiceParamter(viewModel);
            param["action"] = "prevbook";
            var exec = InvokeService(param);
            if (exec)
            {
                if (ServiceProcess.Result.ContainsKey("cache"))
                {
                    var cachebook = Agile.Engine.Utils.ZipUtils.UnZipBase64(ServiceProcess.Result["cache"].ToString());
                    var sdf = string.Format("{0}/cache.sdf", ObjectContext.Environment.Config);
                    Agile.Engine.Utils.IOUtils.WriteStream(cachebook, sdf);
                }
            }

            Message = ServiceProcess.Message;
            return exec;
        }

        public override bool ExecuteUpdate(Hashtable viewModel)
        {
            var data = viewModel["data"] as DataTable;
            var exec = false;
            //addnew
            if (ViewState == Agile.Engine.View.ViewState.AddNew || viewModel["action"].Equals("create"))
            {
                var local = string.Format("{0}/cache.sdf", ObjectContext.Environment.Config);
                //加载模板
                var cache = Agile.Engine.Utils.IOUtils.ReadStream("assembly://Agile.Engine.Config/Agile.Engine.Config.Schema/cache.sdf");
                Agile.Engine.Utils.IOUtils.WriteStream(cache, local);

                //写入新建信息
                var da = new DataAccess() { Context = DataContext, TableName = DataTableName };
                data.Rows[0]["SID"] = Fis.Win.Utils.AppSession.BookID;
                da.Delete(Fis.Win.Utils.AppSession.BookID);
                da.AddNew(data.Rows[0]);
                //写入默认管理员信息
                da.TableName = "TUSER";
                var user = da.Query("FRIGHTS='1'");
                user.Rows[0]["FUSERNAME"] = Fis.Win.Utils.AppSession.UserName;
                user.Rows[0]["FPASSWORD"] = Fis.Win.Utils.AppSession.Password;
                user.Rows[0]["SBOOKID"] = Fis.Win.Utils.AppSession.BookID;
                user.Rows[0]["FSTATUS"] = 1;
                da.Update(user.Rows[0]);

                //写入默认会计模板
                var accSystem = Agile.Engine.Utils.IOUtils.InputStream("assembly://Agile.Engine.Config/Agile.Engine.Config.Schema/Acc_" + data.Rows[0]["FACCSYSTEM"].ToString() + ".xml");
                DataTable accTemp = new DataTable();
                accTemp.ReadXml(accSystem);

                foreach (DataColumn col in accTemp.Columns)
                    col.ColumnName = col.Caption;
                DataAccess accDa = new DataAccess() { Context = DataContext, TableName = "TACCOUNT" };
                DataContext.DefaultModel.Delete(accDa.TableName, "1=1");
                accDa.AddNew(accTemp.Select());


                //上传到服务器
                viewModel["cache"] = Agile.Engine.Utils.ZipUtils.ZipBase64(Agile.Engine.Utils.IOUtils.ReadStream(local));
                viewModel["data"] = Agile.Engine.Utils.ZipUtils.ZipDataTable(data);

                var param = Fis.Win.Utils.AppSession.CreateServiceParamter(viewModel);
                exec = InvokeService(param);
                if (!exec)
                    viewModel["data"] = Agile.Engine.Utils.ZipUtils.UnZipDataTable(param["data"].ToString());
            }
            else
            {
                //服务器端刷新修改信息
                viewModel["data"] = Agile.Engine.Utils.ZipUtils.ZipDataTable(data);
                var param = Fis.Win.Utils.AppSession.CreateServiceParamter(viewModel);

                exec = InvokeService(param);

                if (exec)
                {
                    //客户端刷新修改信息
                    var da = new DataAccess() { Context = DataContext, TableName = DataTableName };
                    var newData = Agile.Engine.Utils.ZipUtils.UnZipDataTable(ServiceProcess.Result["data"].ToString());
                    da.Update(newData.Rows[0]);
                    Fis.Win.Utils.AppSession.InitializeBookInfo();
                }
            }

            View.Message = ServiceProcess.Message;
            return exec;
        }


    }
}
